/*
 * 生成唯一的uuid
 */
var uuid = {
  s4: function () {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  },
  guid: function () {
    // console.log(uuid.s4() + uuid.s4() + uuid.s4() + uuid.s4());
    return (uuid.s4() + uuid.s4() + uuid.s4() + uuid.s4());
  }
}

/*
 * 测评参数
 */
var evalParam = {
  "connect": {
    "cmd": "connect",
    "param": {
      "sdk": {
        "version": 50528256,
        "source": 2,
        "protocol": 2
      },
      "app": {
        "applicationId": "t426",
        "sig": "",
        "timestamp": ""
      }
    }
  },
  "start": {
    "cmd": "start",
    "param": {
      "app": {
        "userId": "wechat",
        "applicationId": "t426",
        "sig": "",
        "timestamp": ""
      },
      "audio": {
        "audioType": "ogg",
        "sampleRate": 16000,
        "channel": 1,
        "sampleBytes": 2
      },
      "request": {
        "typeThres": 0,
        "precision": 1,
        "attachAudioUrl": 1,
        "coreType": "",
        "refText": "",
        "rank": 100,
        "tokenId": uuid.guid(),
      }
    }
  }
}

/*
 * 获取评测的json
 * @param userId 微信用户Id
 * @param refText 测评内容
 * @param coreType 测评类型
 */
function getEvalJson(userId, refText, coreType = 'en.sent.score') {
  evalParam.connect.param.sdk.version = getVersion();
  evalParam.connect.param.sdk.source = source();
  evalParam.start.param.app.userId = userId;
  evalParam.start.param.request.refText = refText;
  evalParam.start.param.request.coreType = coreType;

  return evalParam;
}


/*
 * 生成签名
 */
function getSig(appKey = false, secretKey = false, timeStamp) {
  if (!appkey || !secretKey) {
    return false;
  } else {
    return true;
  }
}

/*
 * 获取source
 */
function source() {
  var ispc = isPC();
  if (ispc) {
    return 7;
  } else {
    var source = getOS;
    return getOS();
  }
}

/*
 * 产生版本号
 */
function getVersion() {
  var vers = '2.0.0';
  var strs = vers.split(".");
  var version = (parseInt(strs[0]) * Math.pow(2, 16) +
    parseInt(strs[1]) * Math.pow(2, 8) +
    parseInt(strs[2])) * Math.pow(2, 8);
  return version;
}

/*
 * 获取操作系统
 * 判断是IOS还是android
 */
function getOS() {
  var u = navigator.userAgent, app = navigator.appVersion;
  var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //g
  var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
  if (isAndroid) {
    return 1;
  }
  if (isIOS) {
    return 5;
  }
}

/*
 * 判断是否是PC端
 * @return false 为手机端, true 为PC端
 */
function isPC() {
  var userAgentInfo = navigator.userAgent;
  var Agents = ["Android", "iPhone",
    "SymbianOS", "Windows Phone",
    "iPad", "iPod"];
  var flag = true;
  for (var v = 0; v < Agents.length; v++) {
    if (userAgentInfo.indexOf(Agents[v]) > 0) {
      flag = false;
      break;
    }
  }
  return flag;
}